# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : spine.py
# Date      : 2018/11/17
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------


__author__ = "lin lingwei"

import maya.cmds as mc

import configs.constants.attribute_constant as ac
import configs.constants.nodeType_constant as nc
import configs.constants.suffix_constant as sc
import configs.constants.body_constant as bc
import configs.constants.global_constant as gc

import scripts.commonCmds as con
import scripts.curveCmds as crc
reload(sc)
import rig.body.bCore as bCore
import rig.jointTool as jt
import rig.sample as sample
import rig.spIkhandle as spIkhandle
reload(sample)
class Spine(bCore.BodyCore):
    
    def __init__(self, localMake, localPrefix,localTag=bc.gSpine):
        super(Spine, self).__init__(localTag=localTag,
                                  localMake=localMake,
                                  localPrefix=localPrefix)
        self.local_colorId = bc.cCenter
        self.out_flw  = None
        self.in_flw = None
    def fkSpine(self,tmpData,jotNumber=10):
        self.localInit(static=True)
        
        fk_jotlist = self.sampleFk(tmpData)
        fk_len = len(fk_jotlist)
        tmpCrv = tmpData["crv"]
        
        sp_crv = con.copyCurve(tmpCrv,name=con.lname(self.localName,bc.gIk,sc.sCrv))
        mc.rebuildCurve(sp_crv, d=1, s=fk_len,tol=0.01, kcp=True)
        mc.rebuildCurve(sp_crv, d=2,tol=0.01, kcp=True)
        
        ikjot_list = jt.crvJointChain(tmpCrv,name=self.localName,number=jotNumber,suffix=True)
        sp_ikobj = spIkhandle.SpIkhandle(self.localName)
        sp_ikobj.create(ikjot_list[0],ikjot_list[-1],sp_crv)
        sp_ikobj.stretch()
        star,end = sp_ikobj.advRotate()
        sp_ikobj.keepVolumn()
        
        mc.parent(star,fk_jotlist[0])
        mc.parent(end,fk_jotlist[-1])
        mc.parent(ikjot_list[0],self.localGrp)
        mc.parent(sp_crv,self.localStatic)
        
        for i,cv in enumerate(mc.ls(sp_crv+".cv[*]",fl=True),1):
            _clu = mc.cluster(cv)[1]
            clu = mc.rename(_clu,con.lname(self.localName,i,sc.sClu))
            clu_grp = con.emptyGroup(clu,["Zero"])
            mc.parent(clu_grp,fk_jotlist[i-1])
            mc.hide(clu)
        
        mc.hide(sp_crv)
        con.transferAttr(sp_ikobj.localBridge,con.lname(self.localName,bc.gFk,1,sc.sCtr),
                         (ac.stretch_switch,ac.keep_volume_switch,ac.keep_volume_ratio))

        self.out_flw_obj(fk_jotlist[-1])
        self.in_flw =fk_jotlist[0]
        return

    def sampleFk(self,tmpData):
        
        self.localInit()
        
        lname = con.lname(self.localName,bc.gFk)
        seq = tmpData["seq"]
        tmp_point = [tmpData [tmp] for tmp in seq]
        jot_list = sample.selectJointChain(tmp_point,lname,colorId=self.local_colorId)
        mc.parent(jot_list[0].replace(sc.sJot,sc.sCtr)+"_Zero",self.localGrp)
        
        self.out_flw = jot_list[-1]
        
        return jot_list
    
    def out_flw_obj(self,obj):
        
        loc = con.objToLocator(obj,name=con.lname(self.localName,"out_flw"))
        self.out_flw = loc
        return loc

if __name__ == '__main__':
    pass